﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Data.Common;
using Mocola.BLL;
using Mocola.Model;

namespace Mocola.Net.Web.ajax
{
    public partial class AddProject : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MembershipCreateStatus status = new MembershipCreateStatus();
            DbTransaction transaction = Databases.MocolaNet.BeginTransaction(IsolationLevel.ReadCommitted);

            try
            {
                
                string pXmxz1 = getRequest("Xmxz1");        // 项目类型1
                string pXmxz2 = getRequest("Xmxz2");        // 项目类型2
                string pXmxz3 = getRequest("Xmxz3");        // 项目类型3
                string pXmxzZdy = getRequest("XmxzZdy");    // 项目类型-自定义

                string pJglx = getRequest("Jglx");          // 结构类型
                string pXmmc = getRequest("Xmmc");          // 项目名称
                string pZdh = getRequest("Zdh");            // 宗地号
                string pDkm = getRequest("Dkm");            // 地块名
                string pAnming = getRequest("Anming");      // 案名
                string pCym1 = getRequest("Cym1");          // 曾用名一
                string pCym2 = getRequest("Cym2");          // 曾用名二
                string pRdoXmmc = getRequest("nRdoXmmc");   // 名称选项

                string pKgtime = getRequest("Kgtime") + "-" + getRequest("Kgtime-m") + "-" + getRequest("Kgtime-d");      // 开工时间
                string pJgtime = getRequest("Jgtime") + "-" + getRequest("Jgtime-m") + "-" + getRequest("Jgtime-d");      // 竣工时间

                string pXmzt = getRequest("Xmzt");          // 项目状态

                string pZdmj = getRequest("Zdmj");          // 占地面积
                string pDsjzmj = getRequest("Dsjzmj");      // 地上建筑面积
                string pDxjzmj = getRequest("Dxjzmj");      // 地下建筑面积
                string pZjzmj = getRequest("Zjzmj");        // 总建筑面积
                string pJrmj = getRequest("Jrmj");          // 计容面积

                string pXmtzqy = getRequest("Xmtzqy");      // 项目投资企业

                string pSgdw = getRequest("Sgdw");          // 施工单位
                string pSjdw = getRequest("Sjdw");          // 设计单位
                string pJldw = getRequest("Jldw");          // 监理单位

                string pXmdz = getRequest("Xmdz");          // 项目地址
                string pBeizhu = getRequest("Beizhu");      // 备注

                int nProjectID = Databases.MocolaNet.Insert(Moc_ProjectInfo.Moc_Project)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.Name, pXmmc)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.PlaceName, pDkm)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.PlotName, pZdh)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.CaseName, pAnming)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.OldName1, pCym1)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.OldName2, pCym2)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.NameChiose, pRdoXmmc)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.Character1, pXmxz1)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.Character2, pXmxz2)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.Character3, pXmxz3)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.CharacterZdy, pXmxzZdy)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.Structure, pJglx)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.StartTime, pKgtime)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.EndTime, pJgtime)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.State, pXmzt)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.PlaceArea, pZdmj)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.OvergroundArea, pDsjzmj)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.BlewgroundArea, pDxjzmj)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.TotalArea, pZjzmj)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.JrArea, pJrmj)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.Corps, pXmtzqy)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.ConstructUnit, pSgdw)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.DesignUnit, pSjdw)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.IntendanceUnit, pJldw)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.Address, pXmdz)
                                 .AddColumn(Moc_ProjectInfo.Moc_Project.Remark, pBeizhu)
                                 .SetTransaction(transaction)
                                 .ExecuteReturnAutoIncrementID(Moc_ProjectInfo.Moc_Project.ProjectID);

                bool blTrees = false;
                
                if (nProjectID!=-1)
                {
                    blTrees = Databases.MocolaNet.Insert(Moc_TreesInfo.Moc_Trees)
                    .AddColumn(Moc_TreesInfo.Moc_Trees.ParentID, 1)
                    .AddColumn(Moc_TreesInfo.Moc_Trees.Name, pXmmc)
                    .AddColumn(Moc_TreesInfo.Moc_Trees.URL, "#")
                    .AddColumn(Moc_TreesInfo.Moc_Trees.Title, pXmmc)
                    .AddColumn(Moc_TreesInfo.Moc_Trees.Target, "")
                    .AddColumn(Moc_TreesInfo.Moc_Trees.Icon, "images/dtree/folder.gif")
                    .AddColumn(Moc_TreesInfo.Moc_Trees.IconOpen, "false")
                    .AddColumn(Moc_TreesInfo.Moc_Trees.IsOpen, "false")
                    .AddColumn(Moc_TreesInfo.Moc_Trees.Type, 1)
                    .AddColumn(Moc_TreesInfo.Moc_Trees.ExtId, nProjectID)
                    .SetTransaction(transaction)
                    .Execute() == 1;
                }


                if (blTrees)
                {
                    Response.Write("新增项目成功！");
                    status = MembershipCreateStatus.Success;
                }
                else
                {
                    Response.Write("新增项目失败！");
                    status = MembershipCreateStatus.ProviderError;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                status = MembershipCreateStatus.ProviderError;
            }
            finally
            {
                if (status == MembershipCreateStatus.Success)
                    transaction.Commit();
                else
                    transaction.Rollback();
                Databases.MocolaNet.CloseConnection(transaction);
            }
        }

        private string getDate(string pMonth)
        {
            string date = "1";
            switch (pMonth)
            {
                case "1":
                    date = "31";
                    break;
                case "2":
                    date = "28";
                    break;
                case "3":
                    date = "31";
                    break;
                case "4":
                    date = "30";
                    break;
                case "5":
                    date = "31";
                    break;
                case "6":
                    date = "30";
                    break;
                case "7":
                    date = "31";
                    break;
                case "8":
                    date = "31";
                    break;
                case "9":
                    date = "30";
                    break;
                case "10":
                    date = "31";
                    break;
                case "11":
                    date = "30";
                    break;
                case "12":
                    date = "31";
                    break;
            }

            return date;
        }

        private string getRequest(string pReqID)
        {
            return string.IsNullOrEmpty(Request.Form[pReqID]) ? string.Empty : Request.Form[pReqID].ToString();
        }
    }
}
